﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Dimeng.ArdisPostProcessor.HomagLC4Adsn.Properties;
using NCalc;

namespace Dimeng.ArdisPostProcessor.HomagLC4Adsn
{
    public class ExpressionDictionary : INotifyPropertyChanged
    {
        public static string GetR41OutputPathExpressionValue(string expression)
        {
            if (string.IsNullOrEmpty(expression))
            {
                throw new Exception("路径不能为空");
            }

            return expression.Replace("[Now]", DateTime.Now.ToString("yyyyMMdd"));
        }

        public static string GetPartExpressionValue(string expression, ReadR41.Part part)
        {
            if (expression == "")
                return "";
            try
            {
                Expression e = new Expression(expression);
                FillExpressionParameters(part, e);
                return e.Evaluate().ToString();
            }
            catch
            {
                return "Error";
            }
        }

        public static string GetPartValueByKey(string keyname, ReadR41.Part part)
        {
            if (keyname == "")
                return "";

            string expression = Dimeng.ArdisPostProcessor.HomagLC4Adsn.Properties.Settings.Default[keyname].ToString();

            if (expression == string.Empty)
                return string.Empty;

            Expression e = new Expression(expression);
            FillExpressionParameters(part, e);
            return e.Evaluate().ToString();
        }

        private static void FillExpressionParameters(ReadR41.Part part, Expression e)
        {
            e.Parameters["PartNo"] = part.PartNo;
            e.Parameters["PartRef"] = part.PartRef;
            e.Parameters["PartRemark"] = part.PartRemark;
            e.Parameters["PartRemark2"] = part.PartRemark2;
            e.Parameters["PartRemark3"] = part.PartRemark3;
            e.Parameters["PartCalc"] = part.PartCalc;
            e.Parameters["PartD"] = part.PartD;
            e.Parameters["PartDrawing"] = part.PartDrawing;
            e.EvaluateFunction += Subfield;
            e.EvaluateFunction += FileExist;
            e.EvaluateFunction += Concat;
        }
        private static void Subfield(string name, FunctionArgs args)
        {
            if (name == "SUBFIELD")
            {
                string a1 = (string)args.Parameters[0].Evaluate();
                //string a2 = (string)args.Parameters[1].Evaluate();
                int a3 = (int)args.Parameters[1].Evaluate();


                Char cha = '|';
                string[] chas = a1.Split(cha);

                if (chas.Length >= a3)
                {
                    args.Result = chas[a3 - 1];
                }
                else
                    args.Result = "";
            }
        }

        private static void FileExist(string name, FunctionArgs args)
        {
            if (name == "FILEEXIST")
            {
                string a1 = (string)args.Parameters[0].Evaluate();

                if (System.IO.File.Exists(a1))
                {
                    args.Result = 1;
                }
                else
                {
                    args.Result = 0;
                }
            }
        }

        private static void Concat(string name, FunctionArgs args)
        {
            if (name == "CONCAT")
            {
                foreach (var s in args.Parameters)
                {
                    args.Result += s.Evaluate().ToString();
                }
            }
        }

        public ExpressionDictionary()
        {
            AddInfo1 = Properties.Settings.Default["AddInfo1"].ToString();
            AddInfo2 = Properties.Settings.Default["AddInfo2"].ToString();
            AddInfo3 = Properties.Settings.Default["AddInfo3"].ToString();
            AddInfo4 = Properties.Settings.Default["AddInfo4"].ToString();
            AddInfo5 = Properties.Settings.Default["AddInfo5"].ToString();
            AddInfo6 = Properties.Settings.Default["AddInfo6"].ToString();
            AddInfo7 = Properties.Settings.Default["AddInfo7"].ToString();
            AddInfo8 = Properties.Settings.Default["AddInfo8"].ToString();
            AddInfo9 = Properties.Settings.Default["AddInfo9"].ToString();
            AddInfo10 = Properties.Settings.Default["AddInfo10"].ToString();
            AddInfo11 = Properties.Settings.Default["AddInfo11"].ToString();
            AddInfo12 = Properties.Settings.Default["AddInfo12"].ToString();
            AddInfo13 = Properties.Settings.Default["AddInfo13"].ToString();
            AddInfo14 = Properties.Settings.Default["AddInfo14"].ToString();
            AddInfo15 = Properties.Settings.Default["AddInfo15"].ToString();
            AddInfo16 = Properties.Settings.Default["AddInfo16"].ToString();
            AddInfo17 = Properties.Settings.Default["AddInfo17"].ToString();
            AddInfo18 = Properties.Settings.Default["AddInfo18"].ToString();
            AddInfo19 = Properties.Settings.Default["AddInfo19"].ToString();
            AddInfo20 = Properties.Settings.Default["AddInfo20"].ToString();
            AddInfo21 = Properties.Settings.Default["AddInfo21"].ToString();
            AddInfo22 = Properties.Settings.Default["AddInfo22"].ToString();
            AddInfo23 = Properties.Settings.Default["AddInfo23"].ToString();
            AddInfo24 = Properties.Settings.Default["AddInfo24"].ToString();
            AddInfo25 = Properties.Settings.Default["AddInfo25"].ToString();
            //SavePath = Properties.Settings.Default["SavePath"].ToString();
            //IsFloatTrimCut = Properties.Settings.Default.IsFloatTrimCut;
            //FloatTrimCutValue = Properties.Settings.Default.FloatTrimCutValue;
            //IsFirstCutZeroNotCut = Settings.Default.IsFirstCutZeroNotCut;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private string savePath;
        public string SavePath
        {
            get { return savePath; }
            set
            {
                savePath = value;
                Properties.Settings.Default["SavePath"] = value;
                if (PropertyChanged != null)
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("SavePath"));
            }
        }
        //private bool isFloatTrimCut;
        //public bool IsFloatTrimCut
        //{
        //    get { return isFloatTrimCut; }
        //    set
        //    {
        //        isFloatTrimCut = value;
        //        Properties.Settings.Default.IsFloatTrimCut = value;
        //        if (PropertyChanged != null)
        //            this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsFloatTrimCut"));
        //    }
        //}

        //private bool isFirstCutZeroNotCut;
        //public bool IsFirstCutZeroNotCut
        //{
        //    get { return isFirstCutZeroNotCut; }
        //    set
        //    {
        //        isFirstCutZeroNotCut = value;
        //        Settings.Default.IsFirstCutZeroNotCut = value;
        //        if (PropertyChanged != null)
        //            this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsFirstCutZeroNotCut"));
        //    }
        //}

        //private double floatTrimCutValue;
        //public double FloatTrimCutValue
        //{
        //    get { return floatTrimCutValue; }
        //    set
        //    {
        //        floatTrimCutValue = value;
        //        Properties.Settings.Default.FloatTrimCutValue = value;
        //        if (PropertyChanged != null)
        //            this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("FloatTrimCutValue"));
        //    }
        //}


        private string addInfo1;
        public string AddInfo1
        {
            get
            {
                return addInfo1;
            }
            set
            {
                addInfo1 = value;
                Properties.Settings.Default["AddInfo1"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo1"));
                }
            }
        }
        private string addInfo2;
        public string AddInfo2
        {
            get
            {
                return addInfo2;
            }
            set
            {
                addInfo2 = value;
                Properties.Settings.Default["AddInfo2"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo2"));
                }
            }
        }
        private string addInfo3;
        public string AddInfo3
        {
            get
            {
                return addInfo3;
            }
            set
            {
                addInfo3 = value;
                Properties.Settings.Default["AddInfo3"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo3"));
                }
            }
        }
        private string addInfo4;
        public string AddInfo4
        {
            get
            {
                return addInfo4;
            }
            set
            {
                addInfo4 = value;
                Properties.Settings.Default["AddInfo4"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo4"));
                }
            }
        }
        private string addInfo5;
        public string AddInfo5
        {
            get
            {
                return addInfo5;
            }
            set
            {
                addInfo5 = value;
                Properties.Settings.Default["AddInfo5"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo5"));
                }
            }
        }
        private string addInfo6;
        public string AddInfo6
        {
            get
            {
                return addInfo6;
            }
            set
            {
                addInfo6 = value;
                Properties.Settings.Default["AddInfo6"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo6"));
                }
            }
        }
        private string addInfo7;
        public string AddInfo7
        {
            get
            {
                return addInfo7;
            }
            set
            {
                addInfo7 = value;
                Properties.Settings.Default["AddInfo7"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo7"));
                }
            }
        }
        private string addInfo8;
        public string AddInfo8
        {
            get
            {
                return addInfo8;
            }
            set
            {
                addInfo8 = value;
                Properties.Settings.Default["AddInfo8"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo8"));
                }
            }
        }
        private string addInfo9;
        public string AddInfo9
        {
            get
            {
                return addInfo9;
            }
            set
            {
                addInfo9 = value;
                Properties.Settings.Default["AddInfo9"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo9"));
                }
            }
        }
        private string addInfo10;
        public string AddInfo10
        {
            get
            {
                return addInfo10;
            }
            set
            {
                addInfo10 = value;
                Properties.Settings.Default["AddInfo10"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo10"));
                }
            }
        }
        private string addInfo11;
        public string AddInfo11
        {
            get
            {
                return addInfo11;
            }
            set
            {
                addInfo11 = value;
                Properties.Settings.Default["AddInfo11"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo11"));
                }
            }
        }
        private string addInfo12;
        public string AddInfo12
        {
            get
            {
                return addInfo12;
            }
            set
            {
                addInfo12 = value;
                Properties.Settings.Default["AddInfo12"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo12"));
                }
            }
        }
        private string addInfo13;
        public string AddInfo13
        {
            get
            {
                return addInfo13;
            }
            set
            {
                addInfo13 = value;
                Properties.Settings.Default["AddInfo13"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo13"));
                }
            }
        }
        private string addInfo14;
        public string AddInfo14
        {
            get
            {
                return addInfo14;
            }
            set
            {
                addInfo14 = value;
                Properties.Settings.Default["AddInfo14"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo14"));
                }
            }
        }private string addInfo15;
        public string AddInfo15
        {
            get
            {
                return addInfo15;
            }
            set
            {
                addInfo15 = value;
                Properties.Settings.Default["AddInfo15"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo15"));
                }
            }
        }
        private string addInfo16;
        public string AddInfo16
        {
            get
            {
                return addInfo16;
            }
            set
            {
                addInfo16 = value;
                Properties.Settings.Default["AddInfo16"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo16"));
                }
            }
        }
        private string addInfo17;
        public string AddInfo17
        {
            get
            {
                return addInfo17;
            }
            set
            {
                addInfo17 = value;
                Properties.Settings.Default["AddInfo17"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo17"));
                }
            }
        }
        private string addInfo18;
        public string AddInfo18
        {
            get
            {
                return addInfo18;
            }
            set
            {
                addInfo18 = value;
                Properties.Settings.Default["AddInfo18"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo18"));
                }
            }
        }
        private string addInfo19;
        public string AddInfo19
        {
            get
            {
                return addInfo19;
            }
            set
            {
                addInfo19 = value;
                Properties.Settings.Default["AddInfo19"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo19"));
                }
            }
        }
        private string addInfo20;
        public string AddInfo20
        {
            get
            {
                return addInfo20;
            }
            set
            {
                addInfo20 = value;
                Properties.Settings.Default["AddInfo20"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo20"));
                }
            }
        }
        private string addInfo21;
        public string AddInfo21
        {
            get
            {
                return addInfo21;
            }
            set
            {
                addInfo21 = value;
                Properties.Settings.Default["AddInfo21"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo21"));
                }
            }
        }
        private string addInfo22;
        public string AddInfo22
        {
            get
            {
                return addInfo22;
            }
            set
            {
                addInfo22 = value;
                Properties.Settings.Default["AddInfo22"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo22"));
                }
            }
        }
        private string addInfo23;
        public string AddInfo23
        {
            get
            {
                return addInfo23;
            }
            set
            {
                addInfo23 = value;
                Properties.Settings.Default["AddInfo23"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo23"));
                }
            }
        }
        private string addInfo24;
        public string AddInfo24
        {
            get
            {
                return addInfo24;
            }
            set
            {
                addInfo24 = value;
                Properties.Settings.Default["AddInfo24"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo24"));
                }
            }
        }
        private string addInfo25;
        public string AddInfo25
        {
            get
            {
                return addInfo25;
            }
            set
            {
                addInfo25 = value;
                Properties.Settings.Default["AddInfo25"] = value;
                if (PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AddInfo25"));
                }
            }
        }

        public static void Save()
        {
            Settings.Default.Save();
        }
    }


}
